package com.ibm.spatiotemp.test.requestServer.redis;

import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.log4j.Logger;

import redis.clients.jedis.JedisPubSub;

/** 
 * @author Yaoliang Chen
 * @version May 15, 2014 
 * Description
 */

public class JedisPubSubHandler extends JedisPubSub{
	public JedisPubSubHandler() {
		super();
		queue = new ConcurrentLinkedQueue<String[]>();	  	
	}

	private static Logger logger = Logger.getLogger(JedisPubSubHandler.class);
	private ConcurrentLinkedQueue<String[]> queue;
	@Override
	public void onMessage(String channel, String message) {
		logger.debug("onMessage:" + channel + "; msg: " + message);
		queue.add(new String[] {channel, message});
	}

	@Override
	public void onPMessage(String pattern, String channel, String message) {
		logger.debug("onPMessage:" + pattern + "; channel: " + channel + "msg: " + message);
		queue.add(new String[] {channel, message});
	}

	@Override
	public void onSubscribe(String channel, int subscribedChannels) {
		logger.debug("onSubscribe:" + channel + "; num: " + subscribedChannels);		
	}

	@Override
	public void onUnsubscribe(String channel, int subscribedChannels) {
		logger.debug("onUnsubscribe:" + channel + "; num: " + subscribedChannels);		
	}

	@Override
	public void onPUnsubscribe(String pattern, int subscribedChannels) {
		logger.debug("onPUnsubscribe:" + pattern + "; num: " + subscribedChannels);		
	}

	@Override
	public void onPSubscribe(String pattern, int subscribedChannels) {
		logger.debug("onPSubscribe:" + pattern + "; num: " + subscribedChannels);
	}
	
	public String[] getMessage(){
		try{
			return queue.remove();
		} catch (NoSuchElementException e){
			return null;
		}
	}
	
}
